package com.lzk.user.controller;

import com.lzk.basic.excption.BusinessException;
import com.lzk.basic.util.JsonResult;
import com.lzk.user.dto.BinderDto;
import com.lzk.user.dto.LoginDto;
import com.lzk.user.serice.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 登录接口
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;
    /**
     * 接收三个参数
     * @param dto
     * @return
     */
    @PostMapping("/account")
    public JsonResult account(@RequestBody LoginDto dto){
        try {
           Map<String,Object> map = loginService.account(dto);
           //返回一个map给jsonResult
            return JsonResult.me().setResultObj(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return JsonResult.me(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"网络繁忙");
        }

    }

    /**
     * 微信登录接口
     * @param code
     * @return
     */
    @GetMapping("/wechat/{code}")
    public JsonResult wechatLogin(@PathVariable("code") String code){
        try {
            return loginService.wechatLogin(code);
        } catch (BusinessException e){
            e.printStackTrace();
            return JsonResult.me(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"网络繁忙");
        }
    }

    /**
     * 微信绑定接口
     * @return
     */
    @PostMapping("/wechat/binder")
    public JsonResult wechatBinder(@RequestBody BinderDto dto){
        try {
            return loginService.wechatBinder(dto);
        } catch (BusinessException e){
            e.printStackTrace();
            return JsonResult.me(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"网络繁忙");
        }
    }
}
